/*
 * This file is part of FinVis.
 *
 * FinVis is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FinVis is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with FinVis.  If not, see <http://www.gnu.org/licenses/>
 *
 * @author Stephen Rudolph
 */

#include <QtGui>
#include <QLabel>
#include <QRadioButton>
#include <QPushButton>
#include <QHBoxLayout>
#include <QButtonGroup>
#include <QMessageBox>

#include "Common.h"
#include "FinRecorder.h"
#include "FinSetupDialog.h"

extern FinRecorder recorder;

FinSetupDialog::FinSetupDialog(QWidget *parent) : QDialog(parent)
{
    setWindowTitle(tr("Confidence Assesment"));
    leastConfident = new QLabel("Least Confident");
    mostConfident = new QLabel("Most Confident");
    promptLabel = new QLabel(
                "How confident are you that you made the best possible decisions in this period?");

    QHBoxLayout *confidenceLayout = new QHBoxLayout;
    confidenceLayout->addWidget(leastConfident);

    confidenceButtonGroup = new QButtonGroup();
    confidenceButtonGroup->setExclusive(true);
    for(int i = 0; i < 8; i++)
    {
        QRadioButton *confidence = new QRadioButton(QString::number(i));
        confidenceLayout->addWidget(confidence);
        confidenceButtonGroup->addButton(confidence);
    }

    confidenceLayout->addWidget(mostConfident);

    okayButton = new QPushButton("Continue");

    QHBoxLayout *promptLayout = new QHBoxLayout;
    promptLayout->addWidget(promptLabel);
    promptLayout->addStretch();

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch();
    buttonLayout->addWidget(okayButton);
    buttonLayout->addStretch();

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(promptLayout);
    mainLayout->addLayout(confidenceLayout);
    mainLayout->addLayout(buttonLayout);

    setLayout(mainLayout);

    connect(okayButton, SIGNAL(pressed()), this, SLOT(confidenceEntered()));
    connect(this, SIGNAL(rejected()), this, SLOT(redo()));

    setFixedHeight(sizeHint().height());
}

void FinSetupDialog::redo()
{
    FinSetupDialog *confidenceAssesment = new FinSetupDialog();
    confidenceAssesment->exec();
}

void FinSetupDialog::confidenceEntered()
{
    if(confidenceButtonGroup->checkedButton() == 0)
    {
        QMessageBox *noneSelected = new QMessageBox(
                    QMessageBox::Warning,
                    "Confidence Not Entered",
                    "Please select a confidence level to continue.",
                    QMessageBox::Ok);
        noneSelected->exec();
    }
    else
    {
        recorder.recordAction(
                    "ConfidenceLevel", confidenceButtonGroup->checkedButton()->text().toInt());
        this->accept();
    }
}
